package loan.calc.context;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author Liusuo.Dong
 * @date 2021-06-26 00:20
 * @description web上下文保持器
 */
public class WebContext {

    private static final ThreadLocal<HttpServletRequest> REQUEST_CONTEXT = new ThreadLocal<>();
    private static final ThreadLocal<HttpServletResponse> RESPONSE_CONTEXT = new ThreadLocal<>();
    private static final ThreadLocal<HttpSession> SESSION_CONTEXT = new ThreadLocal<>();
    private static ServletContext SERVLET_CONTEXT = null;

    public static void setRequest(HttpServletRequest request){
        REQUEST_CONTEXT.set(request);
    }
    public static HttpServletRequest getRequest(){
        return REQUEST_CONTEXT.get();
    }

    public static void setResponse(HttpServletResponse response){
        RESPONSE_CONTEXT.set(response);
    }
    public static HttpServletResponse getResponse(){
        return RESPONSE_CONTEXT.get();
    }

    public static void setSession(HttpSession session){
        SESSION_CONTEXT.set(session);
    }
    public static HttpSession getSession(){
        return SESSION_CONTEXT.get();
    }

    public static void setServletContext(ServletContext servletContext) {
        SERVLET_CONTEXT = servletContext;
    }

    public static ServletContext getServletContext() {
        return SERVLET_CONTEXT;
    }

    public static void clear(){
        REQUEST_CONTEXT.remove();
        RESPONSE_CONTEXT.remove();
        SESSION_CONTEXT.remove();
    }

}
